(function (angular) {
    var module = angular.module("userApp", ["tm.pagination","userFilter"]);
    module.controller("userCtrl", ["$scope", "$http", function ($scope, $http) {
        // 初始化
        $scope.init = function () {
            $scope.name = "";
            $scope.account = "";
            $scope.citID = "";
            $scope.sex = "1";
            $scope.role = "1";
            $scope.email = "";
            $scope.mobile = "";
            $scope.dep = "";
            $scope.useMark = "1";
        };
        $scope.init();
        $scope.params={
            name:"",
            mobile:"",
        }
        // 分页参数
        $scope.page = {
            currentPage: 1,
            totalItems: 0,
            itemsPerPage: 5,
            pagesLength: 5,
            onChange: function () {
                $scope.getUserList($scope.page.currentPage, $scope.page.itemsPerPage);
            }
        };
        $scope.url = "http://127.0.0.1:8080/lx/";
        // 获取用户列表
        $scope.getUserList = function (currPage, size) {
            if (!currPage && currPage != 0) {
                currPage = $scope.page.currentPage;
            }
            currPage -= 1;
            if (!size) {
                size = $scope.page.itemsPerPage;
            }
            var params = {
                page: currPage,
                size: size ? size : $scope.page.itemsPerPage,
            };
            angular.extend(params,$scope.params);
            layer.load();
            $http({
                method: "post",
                url: $scope.url + "getUserListPages",
                data: params,
            }).success(function (res) {
                layer.closeAll("loading");
                // console.log(res);
                if (res.code === 0) {
                    $scope.page.content = res.data.content;
                    $scope.page.totalItems = res.data.totalElements;
                    $scope.page.currentPage = currPage + 1;
                    $scope.page.itemsPerPage = size;
                }
            })
        };
        // 删除
        $scope.delete = function (id) {
            var cf = confirm("是否删除?");
            if (cf) {
                layer.load();
                $http({
                    method: "post",
                    url: $scope.url + "deleteUser",
                    data: id,
                }).success(function (res) {
                    layer.closeAll("loading");
                    if (res.code === 0) {
                        layer.alert("删除成功");
                        $scope.getUserList($scope.page.currentPage, $scope.page.itemsPerPage);
                    } else {
                        layer.alert("删除失败");
                    }
                })
            }
        };
        // 编辑
        $scope.edit = function (id) {
            // $(".user").css("display","block");
            $scope.getDepList();
            $(".user").fadeIn();
            $scope.init();
            if (id) {
                $scope.headName = "编辑用户";
                layer.load();
                $http({
                    method: "post",
                    url: $scope.url + "getUserById",
                    data: id,
                }).success(function (res) {
                    layer.closeAll("loading");
                    if (res.code === 0) {
                        // console.log(res);
                        $scope.id = res.data.id;
                        $scope.name = res.data.perName;
                        $scope.account = res.data.account;
                        $scope.citID = res.data.citID;
                        $scope.sex = res.data.sexCode;
                        $scope.role = res.data.role;
                        $scope.email = res.data.email;
                        $scope.mobile = res.data.mobile;
                        $scope.useMark = res.data.useMark;
                        $scope.dep = res.data.department?res.data.department.id:"";
                    }
                })
            } else {
                $scope.headName = "添加用户";
            }
        };
        // 保存
        $scope.submit = function () {
            var msg = "添加";
            if ($scope.id) {
                msg = "编辑";
            }
            layer.load();
            $http({
                method: "put",
                url: $scope.url + "saveUser",
                data: {
                    id: $scope.id,
                    perName: $scope.name,
                    account: $scope.account,
                    citID: $scope.citID,
                    sexCode: $scope.sex,
                    role: $scope.role,
                    email: $scope.email,
                    mobile: $scope.mobile,
                    useMark: $scope.useMark,
                    department: $scope.dep ? {id: $scope.dep} : null,
                }
            }).success(function (res) {
                layer.closeAll("loading");
                if (res.code === 0) {
                    layer.alert(msg + "成功");
                    $(".user").css("display", "none");
                    $scope.getUserList($scope.page.currentPage, $scope.page.itemsPerPage);
                } else {
                    layer.alert(msg + "失败");
                }
            })
        };
        // 获取部门下拉
        $scope.getDepList = function () {
            layer.load();
            $http({
                method: "get",
                url: $scope.url + "getDepartmentSelect",
            }).success(function (res) {
                layer.closeAll("loading");
                if (res.code === 0) {
                    // console.log(res);
                    $scope.depList = res.data;
                }
            })
        };
        // 状态改变
        $scope.changeStatus = function (id, useMark) {
            layer.load();
            $http({
                method: "post",
                url: $scope.url + "changeUserMark",
                data: {
                    id: id,
                    status: useMark,
                },
            }).success(function (res) {
                layer.closeAll("loading");
                if (res.code === 0) {
                    console.log(res);
                    $scope.useMark = res.data.useMark;
                    $scope.getUserList($scope.page.currentPage, $scope.page.itemsPerPage);
                }
            })
        };
        // 搜索查询
        $scope.search=function () {
            $scope.getUserList($scope.page.currentPage, $scope.page.itemsPerPage);
        }
    }]);
    var userFilter = angular.module("userFilter", []);
    // 身份证长度截取
    userFilter.filter("sub", function () {
        return function (str) {
            if (str && str.length >= 5) {
                return str.substr(0, 5) + "...";
            } else {
                return str;
            }
        }
    })
})(angular);